package com.wuye.service;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.wuye.dao.TousuDaoImpl;
import com.wuye.dao.UsersDaoImpl;
import com.wuye.entity.Tousu;

@Service("tousuService")
public class TousuService {
	@Resource
	private TousuDaoImpl tousuDao;
	@Resource
	private UsersDaoImpl userDao;

	public void save(Tousu tousu) {
		tousu.setType(0);
		tousu.setShijian(new Date());
		tousuDao.save(tousu);
	}

	public void del(String str) {
		String[] sz = str.split(",");
		for (int i = 0; i < sz.length; i++) {
			Tousu tousu = new Tousu();
			tousu.setId(Integer.parseInt(sz[i]));
			tousuDao.del(tousu);
		}
	}

	public void update(String str) {
		String[] sz = str.split(",");
		for (int i = 0; i < sz.length; i++) {
			Tousu	tousu=new Tousu();
			tousu = tousuDao.getObject(Integer.parseInt(sz[i]));
			tousu.setShijian(new Date());
			tousu.setType(1);
			tousuDao.update(tousu);
		}
	}

	public Map getList(int offset, int length) {
		String[] title = { "id", "neirong", "shijian", "username" };
		return tousuDao
				.getlist(
						title,
						"select t.id,t.neirong,substring(t.shijian,1,10),t.users.name from Tousu t where t.type=0",
						offset, length);
	}

	public Map getSearchList(Tousu tousu, int offset, int length)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		String[] title = { "id", "neirong", "shijian", "username" };
		sb.append("select t.id,t.neirong,substring(t.shijian,1,10),t.users.name from Tousu t ");
		if (tousu.getType() != 2) {
			sb.append("where t.type=" + tousu.getType());
		}
		return tousuDao.getlist(title, sb.toString(), offset, length);
	}

	public UsersDaoImpl getUserDao() {
		return userDao;
	}

	public void setUserDao(UsersDaoImpl userDao) {
		this.userDao = userDao;
	}

	public TousuDaoImpl getTousuDao() {
		return tousuDao;
	}

	public void setTousuDao(TousuDaoImpl tousuDao) {
		this.tousuDao = tousuDao;
	}
}
